<?php


namespace app\shop\service\order;


use app\shop\model\order\OrderModel;
use app\shop\model\user\DesignScoreModel;
use think\facade\Cache;
use think\facade\Db;

class EvaluateService
{
    protected $key = "evaluate:remind:%d";
    protected $redis;

    public function __construct()
    {
        $this->redis = Cache::store('redis')->handler();
    }

    /**
     *聊天框内是否提示客户去评价订单
     * @param $orderId
     * @return bool
     * @throws \Exception
     */
    public function getOrderEvaluateRemindStatus($orderId)
    {
        try {
            $orderInfo = OrderModel::find($orderId);
            if (!$orderInfo) {
                return false;
            }
            if ($orderInfo['order_status'] !== OrderModel::UNDERWAY) {
                return false;
            }
            // 订单创建7天内不提醒
            if ($orderInfo->getData('create_time') + 7 * 86400 < time()) {
                return false;
            }
            // 订单是否评价
            $designScore = DesignScoreModel::where('order_id', $orderId)->where('type', 0)->find();
            if ($designScore) {
                return false;
            }
            // 今天是否已经提醒
            if ($this->getTodayIsEvaluateRemind($orderId)) {
                return false;
            }
            return true;
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

    /**
     * 设置订单评价今日已经提醒过
     * @param $orderId
     * @return bool
     * @throws \Exception
     */
    public function setTodayEvaluateRemind($orderId)
    {
        try {
            $currentTime = time();
            $tomorrowTime = strtotime(date('Ymd')) + 86399;
            $expireTime = $tomorrowTime - $currentTime;

            $key = sprintf($this->key, $orderId);

            $result = $this->redis->set($key, true, ['NX', 'EX' => $expireTime]);
            return $result ? true : false;
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

    /**
     * 判断订单今日是否已经提醒过
     * @param $orderId
     * @return bool
     * @throws \Exception
     */
    public function getTodayIsEvaluateRemind($orderId)
    {
        try {
            $key = sprintf($this->key, $orderId);

            $result = $this->redis->get($key);
            return $result ? true : false;
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

    /**
     * 已完成订单是否有需要评价的
     * @param $userId
     * @return array|mixed|Db|\think\Model|null
     * @throws \Exception
     */
    public function overOrderRemind($userId)
    {
        try {
            $time = 0;
            $orderInfo = Db::name('order')
                ->alias('o')
                ->leftJoin('design_score d', 'd.order_id = o.id')
                ->where('o.user_id', $userId)
                ->where('o.finalize_time', '>', $time)
                ->where('o.order_status', 2)
                ->where('d.type', 1)
                ->whereNull('d.id')
                ->order('o.id', 'desc')
                ->field('o.id,o.order_name')
                ->find();
            return $orderInfo;
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }
}